const Router = require('koa-router')
// 引入guidance数据库模块
const Assess = require('../../models/Assess')
// 创建router
const AssessRouter = new Router()
/**
 * @route POST api/Assess/add
 * @description 新增评价
 * @access      接口公开
 */
AssessRouter.post('/add',async(ctx) =>{
    const {flowerName,commodityOrders_id,userName,isAdmin,message} = ctx.request.body
    // 获取特定条件的咨询
    const data = await Assess.findOne({commodityOrders_id})
    if(!data){
            const newAssess = new Assess({
                flowerName,
                commodityOrders_id,
                userName,
                messageAry:[{isAdmin,message,Date:new Date()}]
            })
            // 存储到数据库
            newAssess.save()
            // 向客户端返回数据
            ctx.status = 200
            ctx.body = {
                status:ctx.status,
                success:true,
                data:[{isAdmin,message,Date:new Date()}],
                msg:'评论成功！'
            }
        }else{
         // 向客户端返回数据
         ctx.status = 403
         ctx.body = {
             status:ctx.status,
             success:false,
             msg:'每个订单只能评论一次！'
            }
        }
    }
)
/**
 * @route POST api/Assess/reply
 * @description 对评价进行回复
 * @access      接口公开
 */
AssessRouter.post('/reply',async(ctx) =>{
    const {commodityOrders_id,isAdmin,message} = ctx.request.body
    // 获取特定条件的咨询
    const data = await Assess.findOne({commodityOrders_id})
    // 对评论进行更新
    const updateMessage = {messageAry:[...data.messageAry,{isAdmin,message,Date:new Date()}]}
    // 对管理员数据库进行更新
    await Assess.findByIdAndUpdate(data._id.toString(),updateMessage)
        // 向客户端返回数据
        ctx.status = 200
        ctx.body = {
            status:ctx.status,
            success:true,
            msg:'回复评论成功！'
        }
    }
)
/**
 * @route POST api/Assess/all
 * @description 获取该鲜花所有的评论
 * @access      接口公开
 */
AssessRouter.post('/all',async(ctx) =>{
    let flowerName = ctx.request.body.flowerName
    // 获取所有的咨询
    const data = await Assess.find({flowerName})
    ctx.status = 200
         ctx.body = {
             status:ctx.status,
             success:true,
             data,
             msg:'该商品所有评论返回成功'
         }
})
/**
 * @route GET api/Assess/allAssess
 * @description 获取所有鲜花的所有的评论
 * @access      接口公开
 */
AssessRouter.get('/allAssess',async(ctx) =>{
    // 获取所有的咨询
    const data = await Assess.find({})
    ctx.status = 200
         ctx.body = {
             status:ctx.status,
             success:true,
             data,
             msg:'所有评论返回成功'
         }
})
module.exports = AssessRouter